from __future__  import division
from euclid      import Point3
from geometry    import *
from collections import namedtuple
from itertools   import combinations
from canvas      import *
from scene       import *
from datetime    import datetime

X = 400
Y = 300
Z = 800

tk    = Tkinter.Tk()
image = ScreenImage(tk, color=(205,186,150))

scene = Scene()

#scene.append(
#    Solid(
#        Pyramid( Point3(X/2, Y, Z),
#             Point3(X, Y, 2*Z),
#             Point3(X/2, 0, 3*Z/2),
#             Point3(0, Y, 2*Z)
#            ),
#        Color(10, 10, 10),
#        0.2,
#        0.8,
#        1.2)
#    )

scene.append(
    Solid(
        Sphere(
            Point3(X/2, Y/2, Z),
            Y/2
            ),
        Color(10, 10, 10),
        0,
        0.9,
        1.4
        )
    )

#scene.append(
#    Solid(Plane(Vector3(0, 0, -1), 0.0), Color(0, 0, 0), 0, 0.9, 3))

scene.append(
    Solid(
        Pyramid(
            Point3(X/2+X/2, 3*Y/2, Z+Z/2),
            Point3(X+X/2  , 3*Y/2, 2*Z+Z/2),
            Point3(X/2+X/2, -Y/2 , 3*Z/2+Z/2),
            Point3(0+X/2  , 3*Y/2, 2*Z+Z/2)
            ),
        Color(0, 150, 160),
        0.5,
        0,
        0)
    )

scene.append(
    Solid(
        Sphere(
            Point3(0, 0, 3*Z),
            Y
            ),
        Color(80, 80, 80),
        0.99
        )
    )

scene.append(
    Solid(
        Plane(Point3(0, 2*Y, 0), Vector3(0, -1, 0)),
        Color(20,20,60),
        0)
    )

scene.light = Point3(0, -Y, 0)

eye = Point3(X/2, Y/2, -Z)
for x in range(X):
    for y in range(Y):
        #print "On point {0}, {1}".format(x, y)
        grid_point = Point3(x,y,0)
        ray = Ray3(eye, grid_point)
        color = scene.ray_color(ray)
        if color is not None:
            image.putpixel((x, y), color)

print "Image done"

image.update()

timestring = datetime.now().strftime("%Y-%m-%d_%H-%M")
image.save("raytracing-" + timestring + ".png")

tk.mainloop()
